package com.lucky.contract.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class Browser {
	/**
	 * 是否移动端打开
	 * */
    public static Boolean isMobile(HttpServletRequest request)
    {
        String userAgent = request.getHeader("user-agent");
        if (null == userAgent)
        {
            userAgent = "";
        }
        Matcher matcherPhone = phonePat.matcher(userAgent);
        Matcher matcherTable = tablePat.matcher(userAgent);
        if (matcherPhone.find() || matcherTable.find()) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 获取浏览器内核名称
     * */
    public static String getBrowserName(HttpServletRequest request)
    {
    	String agent = request.getHeader("user-agent").toLowerCase();

    	if (agent.contains("micromessenger")) {
    		return "wechat";
    	} else if (agent.contains("ucbrowser")) {
    		return "uc";
    	} else if (agent.contains("mqqbrowser") && agent.contains(" qq")) {
    		return "qqapp";
    	} else if (agent.contains("mqqbrowser")) {
    		return "qqbrowser";
    	} else if (agent.contains("msie")) {
    		return "ie";
    	} else if (agent.contains("chrome")) {
    		return "chrome";
    	} else if (agent.contains("firefox")) {
    		return "firefox";
    	} else if (agent.contains("opera")) {
    		return "opera";
    	}
        return "others";
    }

	// \b 是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔),
	// 字符串在编译时会被转码一次,所以是 "\\b"
	// \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)
	static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"
	       +"|windows (phone|ce)|blackberry"
	       +"|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
	       +"|laystation portable)|nokia|fennec|htc[-_]"
	       +"|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
	static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"
	       +"|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
	//移动设备正则匹配：手机端、平板
	static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);
	static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);
}
